package com.atxwq.shop.user.controller;

import com.atxwq.shop.model.dto.h5.UserLoginDto;
import com.atxwq.shop.model.dto.h5.UserRegisterDto;
import com.atxwq.shop.model.entity.user.UserInfo;
import com.atxwq.shop.model.vo.common.Result;
import com.atxwq.shop.model.vo.common.ResultCodeEnum;
import com.atxwq.shop.model.vo.h5.UserInfoVo;
import com.atxwq.shop.user.service.UserInfoService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 用户注册控制器
 * @Date 2025/5/22 13:19
 * @Created by 12336
 */

@RestController
@RequestMapping("api/user/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 注册用户
     * @param userRegisterDto
     * @return
     */
    @PostMapping("register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {
         userInfoService.register(userRegisterDto);
         return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登录
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
        String token = userInfoService.login(userLoginDto);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取当前登录用户信息
     * @param request
     * @return
     */
    @GetMapping("auth/getCurrentUserInfo")
    public Result getCurrentUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }

}
